//
//  NSArray+Sequence.m
//  
//
//  Created by lwx376629 on 16/11/22.
//  Copyright © 2016年 Capfer. All rights reserved.
//

#import "NSArray+Sequence.h"

@implementation NSArray (Sequence)
/**
 *  给数组排序
 *
 *  @param upward  是否升序(否为降序)
 *
 *  @return 排序后的数组
 */
- (NSArray *)sequenceWithUpward:(BOOL)upward {
    
    NSMutableString *outputBefore = [[NSMutableString alloc] init];
    
    for (NSString *str in self) {
        
        [outputBefore appendFormat:@"%@,",str];
        
    }
    
    NSArray *array = [self sortedArrayUsingComparator:cmptr];
    
    NSMutableString *outputAfter = [[NSMutableString alloc] init];
    
    for (NSString *str in array) {
        
        [outputAfter appendFormat:@"%@,",str];
        
    }
    
    if (upward) {
        
        return array;
        
    } else {
        
        return (NSArray *)[[array reverseObjectEnumerator] allObjects];
        
    }
    
}
NSComparator cmptr = ^(id obj1, id obj2){
    
    if ([obj1 integerValue] > [obj2 integerValue]) {
        
        return (NSComparisonResult)NSOrderedDescending;
        
    }
    if ([obj1 integerValue] < [obj2 integerValue]) {
        
        return (NSComparisonResult)NSOrderedAscending;
        
    }
    
    return (NSComparisonResult)NSOrderedSame;
    
};
/**
 *  数组倒叙
 *
 *  @return       倒序后的数组
 */
- (NSArray *)inverse {
    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:self.count];
    for (NSInteger i = self.count - 1; i >= 0; i --) {
        [arrayM addObject:self[i]];
    }
    return [arrayM mutableCopy];
}
@end
